<template>
  <div class="home">
    <div class="detail_box">
      <!-- 菜单 -->
      <div class="tab_menu">
        <div>
          <span v-for="(item,index) in menus" :class="{'choose':index===tabIndex}" @click="chooseTab(index)">{{item}}订单</span>
        </div>
      </div>
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>订单号：</label>
            <el-input type="text" placeholder="请输入订单号" v-model="keyword" clearable></el-input>
          </div>
          <div class="operate_item"  v-if="!$route.query.userId">
            <label>用户：</label>
            <userSelect @listenToUser="getUserId" :uId="userId"/>
          </div>


          <div class="operate_item">
            <label>时间：</label>
            <el-date-picker
              v-model="proDate"
              type="datetimerange"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="searchData">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&btn.add">
          <span class="operate_btn search_btn" @click="isCreate=true">生成订单</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="flex_box">
        <div class="detail_part">
          <div class="table_box">
            <el-table
              border
              height="620"
              v-loading="loading"
              :header-cell-style="tableHeader"
              ref="multipleTable"
              :data="tableInfo"
              tooltip-effect="dark"
              highlight-current-row
              style="width: 100%;">

              <el-table-column
                width="160"
                sortable="custom"
                prop="createTime"
                label="创建时间">
                <template slot-scope="scope">
                  <p style="text-align: left">{{formatTime(scope.row.createTime)}}</p>
                </template>
              </el-table-column>
              <el-table-column
                width="230"
                prop="changeOrderNo"
                label="订单号">
              </el-table-column>
              <el-table-column
                prop="wxName"
                label="用户名">
                <template slot-scope="scope">{{scope.row.wxName?scope.row.wxName:'——'}} {{scope.row.remark ? `(${scope.row.remark})`:''}}</template>
              </el-table-column>

              <el-table-column
                width="100"
                prop="userId"
                label="用户ID">
              </el-table-column>

<!--              <el-table-column-->
<!--                prop="typeName"-->
<!--                label="支付类型">-->
<!--                <template slot-scope="scope">{{scope.row.typeName?scope.row.typeName:'其他'}}</template>-->
<!--              </el-table-column>-->

              <el-table-column
                prop="operateUserName"
                label="操作人">
                <template slot-scope="scope">{{scope.row.operateUserName?scope.row.operateUserName:'——'}}</template>
              </el-table-column>

              <el-table-column
                prop="enable"
                label="状态">
                <template slot-scope="scope"><span :style="{color:scope.row.enable===2 ? '#6687f9' :(scope.row.enable===1 ? '#67c430' :'#e20000')}">{{scope.row.enable===2 ? '待确认' :(scope.row.enable===1 ? '已完成' :'已取消')}}</span></template>
              </el-table-column>

              <el-table-column
                label="操作"
                fixed="right"
                v-if="btn&&(btn.confirm || btn.detail)"
                width="220">
                <template slot-scope="scope">
                  <div class="table_btn">
                    <span class="change_info" @click="openPopup(scope.row)" v-if="btn.detail">详情</span>
                    <span class="change_info" @click="confirm(scope.row)" v-if="btn.confirm && (scope.row.enable === 2)">确认</span>
                    <span class="change_info delete_btn" v-if="btn.cancel&&(scope.row.enable===2)" @click="deleteItem(scope.row)">取消</span>
                  </div>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="record_page">
            <span class="page_count">共<i>{{totals}}</i>条</span>
            <el-pagination
              @size-change="sizeChange"
              @current-change="getTableInfo"
              background
              :current-page="currentPage"
              :page-sizes="[15, 20, 30, 40]"
              :page-size="pageSize"
              layout="prev, pager, next, sizes,jumper"
              :total="totals">
            </el-pagination>
          </div>
        </div>

      </div>

    </div>
    <createOrder v-if="isCreate" @listenToChild="listenToEdit"></createOrder>
    <detail v-if="isPop" :id="orderId" :isConfirm="isConfirm" @listenToChild="listenToEdit"></detail>
  </div>
</template>

<script>

import detail from './detail'
import userSelect from '../../../components/user/index.vue'
import createOrder from "./createOrder";
import commons from "@/utils/common";
import { listData,cancelOrder} from "@/api/data/user/change";
import {getBtn} from "@/utils/btnAuth";

export default {
    components: {
      detail,
      createOrder,
      userSelect
    },
    data() {
      return {
        menus:['待确认','已完成'],
        mIndex:2,
        proDate:'',
        keyword:'',
        userId:'',
        currentPage: 1,
        pageSize: 15,
        totals: 0,
        loading: false,
        tableInfo:[],
        isPop: false,
        orderId: '',
        orderNo:'',
        uId:'',
        oId:'',
        isCreate:false,
        isSend:false,
        btn:{},
        update:false,
        isRemark:false,
        remark:'',
        types:1,
        type:'',
        tabIndex:0,
        isConfirm:false
      }
    },
    mounted() {
      //用户列表过来
      if(this.$route.query.userId) {
        this.userId = parseInt(this.$route.query.userId);
      }
      this.btn = getBtn();
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    },
    methods: {
      chooseTab(index){
        this.mIndex = index === 0 ? 2 : -1;
        this.tabIndex = index;
        this.getTableInfo(1);
      },

      // 获取列表信息
      getTableInfo(page) {
        let that = this;
        that.loading = true;
        that.currentPage = page;
        let queryParams = {
          limit:that.pageSize,
          page:page,
          changeOrderNo:that.keyword,
          userId:that.userId,
          enable:that.mIndex,
          startDate:that.proDate?that.proDate[0]:'',
          endDate:that.proDate?that.proDate[1]:'',
        }
        listData(queryParams).then(res => {
          that.tableInfo = res?res.list:[];
          that.totals = res?res.count:0;
          that.loading = false;
        }).catch((e) => {
          that.loading = false;
        });
      },

      // 重置
      resetTable() {
        this.pageSize = 15;
        this.keyword = '';
        this.userId = '';
        this.proDate = '';
        this.searchData()
      },

      searchData(){
        this.isPop = false;
        this.getTableInfo(1)
      },

      getUserId(e){
        this.userId = e;
      },
      // 删除
      deleteItem(obj) {
        let that = this;
    let name = (obj.wxName ? obj.wxName : '') + (obj.remark ? '(' + obj.remark  + ')' : '')
        that.$confirm('确定取消' + name + '变更单信息?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return cancelOrder(obj.id);
        }).then(() => {
          that.getTableInfo(that.currentPage);
          that.$message.success({
            message: '删除成功！',
            duration: 1500
          });
        }).catch(() => {
        });
      },

      // 打开弹窗
      openPopup(obj,type) {
        this.isPop = true;
        this.isConfirm = type===2;
        this.orderId = obj.changeOrderNo;
      },

      sendPopup(obj){
        this.isSend = true;
        this.oId = obj.orderNo;
        this.uId = obj.userId;
      },

      // 确认
      confirm(obj) {
        let that = this;
        that.isPop = true;
        that.orderId = obj.changeOrderNo;
        that.isConfirm = true;
      },

      listenToEdit(e){
        let that = this;
        that.isCreate = false;
        that.isPop = false;
        that.isConfirm = false;
        if (e){
          that.getTableInfo(that.currentPage)
        }
      },

      formatTime(time){
        return commons.formatTime(time)
      },

      sizeChange(val){
        this.pageSize =val;
        this.getTableInfo(1)
      },

      // 设置table标题居中
      tableHeader({row, column, rowIndex, columnIndex}) {
        return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
      },
    }
  }
</script>

<style scoped>
.flex_box {
  display: flex;
}
.detail_part {
  width: 100%;
}
</style>
